package cc.mrbird.febs.power.controller;


import cc.mrbird.febs.common.utils.HttpUtils;
import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.power.entity.AdminLogin;
import cc.mrbird.febs.power.service.IAdminLoginService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author MrBird
 */
@RestController
@RequestMapping("/power/admin-login")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AdminLoginController {

    private final IAdminLoginService iAdminLoginService;

    private SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取登录记录
     * @return
     */
    @GetMapping("/info/list")
    public R getAdminLogin() {
        return R.ok(iAdminLoginService.list());
    }

    /**
     * 外部新增记录
     * @param name
     * @return
     */
    @PostMapping
    public R saveAdminLogin(String name) {
        String result = HttpUtils.get("http://api.ip.la/cn?json","");
        JSON.parseObject(result).getJSONObject("location");
        AdminLogin adminLogin = new AdminLogin(name,new BigDecimal(JSON.parseObject(result).getJSONObject("location").get("latitude").toString()),new BigDecimal(JSON.parseObject(result).getJSONObject("location").get("longitude").toString()),
                JSON.parseObject(result).getJSONObject("location").get("country_name")+""+JSON.parseObject(result).getJSONObject("location").get("province")+""+JSON.parseObject(result).getJSONObject("location").get("city"),
                JSON.parseObject(result).get("ip").toString(),sdf3.format(new Date()));
        return R.ok(iAdminLoginService.save(adminLogin));
    }

}
